package com.lhj.springboot3.core.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author lhj
 * @Date 2024/6/30 11:13
 */
@Service
public class PublisherEvent implements ApplicationEventPublisherAware {

    /**
     * 底层发送事件用的组件，SpringBoot会通过ApplicationEventPublisherAware接口，自动注入
     * 事件是广播出去的，所有监听时间的监听器都会收到
     */
    private ApplicationEventPublisher applicationEventPublisher;
    
    public void publishEvent(ApplicationEvent event) {
        applicationEventPublisher.publishEvent(event);
    }

    /**
     * 注入ApplicationEventPublisher 自动注入，把真正发事件的底层组件给我们注入进来
     * @param applicationEventPublisher event publisher to be used by this object
     */
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
}
